package com.tma.customlistview;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.tma.customlistview.xml.Song;

public class SongsAdapter extends BaseAdapter {

    private Activity activity;
    private List<Song> data;
    private LayoutInflater inflater;
    public ImageLoader imageLoader;

    public SongsAdapter(Activity a, List<Song> songs) {
        activity = a;
        data = songs;
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader = new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;

        if (convertView == null)
            view = inflater.inflate(R.layout.list_row, null);
 
        TextView title = (TextView)view.findViewById(R.id.title); 
        TextView artist = (TextView)view.findViewById(R.id.artist);
        TextView duration = (TextView)view.findViewById(R.id.duration);
        ImageView thumbImage=(ImageView)view.findViewById(R.id.list_image);
 
        Song song = data.get(position);
 
        title.setText(song.getTitle());
        artist.setText(song.getArtist());
        duration.setText(song.getDuration());
        imageLoader.displayImage(song.getThumbUrl(), thumbImage);

        return view;
    }
}
